package com.vast.admin.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.vast.common.core.domain.entity.SysRole;
import com.vast.common.core.domain.entity.SysUser;
import com.vast.common.utils.StringUtils;
import com.vast.system.service.ISysRoleService;
import com.vast.system.utils.Constant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Service
public class AuthService {

    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private ManagerCompanyDataService managerCompanyDataService;

    /**
     * 获取用户企业权限数据
     * 返回null为超级管理员
     * 返回空集合（size为0）为采集员
     * @param manager
     * @return
     */
    @Deprecated
    public List<Long> getManagerComanyIds(SysUser manager) {
        List<Long> companyIds;
        List<SysRole> roles;
        if (manager.getRoleIdList().length > 0) {
            roles = roleService.getByRoleIds(manager.getRoleIdList());
        } else {
            roles = new ArrayList<>();
        }
        if (isContain(roles, Constant.SUPER_ADMIN_ALIAS)) {
            //超级管理员
            companyIds = null;
        } else if (isContain(roles, Constant.SYSTEM_MANAGER_ALIAS)) {
            //管理员
//            companyIds = managerCompanyDataService.getCompanyIdsByManagerId(manager.getUserId());
//            if (companyIds == null) {
//                companyIds = new ArrayList<>();
//            }
//            companyIds.add(manager.getCompanyId());
            companyIds = null;
        } else if (isContain(roles, Constant.COMPANY_EMPLOYEE_ALIAS)) {
            //业务员
            companyIds = managerCompanyDataService.getCompanyIdsByManagerId(manager.getUserId());
            if (companyIds == null) {
                companyIds = new ArrayList<>();
            }
//            if (!companyIds.contains(manager.getCompanyId())) {
//                companyIds.add(manager.getCompanyId());
//            }
        } else if (isContain(roles, Constant.PARTNER_ALIAS)) {
            //合伙人
            companyIds = managerCompanyDataService.getCompanyIdsByManagerId(manager.getUserId());
            if (companyIds == null) {
                companyIds = new ArrayList<>();
            }
//            if (!companyIds.contains(manager.getCompanyId())) {
//                companyIds.add(manager.getCompanyId());
//            }
        } else if (isContain(roles, Constant.COMPANY_ADMIN_ALIAS)) {
            //企业管理员
            companyIds = Arrays.asList(manager.getCompanyId());
        } else if (isContain(roles, Constant.COMPANY_SALESMAN_ALIAS)) {
            //企业采集员
            companyIds = new ArrayList<>();
        } else {
            //未知角色
            companyIds = new ArrayList<>();
        }
        return companyIds;
    }

    /**
     * 权限控制
     * @param queryWrapper
     * @param manager 当前登录用户
     * @param companyCol 公司字段名称
     * @param managerCol 个人字段名称
     * @param <T>
     */
    @Deprecated
    public <T> void setAuthInfo(QueryWrapper<T> queryWrapper, SysUser manager, String companyCol, String managerCol) {
        List<Long> companyIds = getManagerComanyIds(manager);
        if (companyIds == null) {

        } else if (companyIds.size() == 0) {
            if (StringUtils.isEmpty(managerCol)) {
                queryWrapper.eq("1", "2");
            } else {
                queryWrapper.eq(managerCol, manager.getUserId());
            }
        } else if (companyIds.size() > 0) {
            if (StringUtils.isEmpty(managerCol) && StringUtils.isEmpty(companyCol)) {
                queryWrapper.eq("1", "2");
            } else {
                queryWrapper.and(tQueryWrapper ->
                        tQueryWrapper.eq(StringUtils.isNotEmpty(managerCol), managerCol, manager.getUserId())
                                .or()
                                .in(StringUtils.isNotEmpty(companyCol), companyCol, companyIds)
                );
            }
        }
    }

    /**
     * 是否包含角色别名
     * @param roles
     * @param roleAlias
     * @return
     */
    private boolean isContain(List<SysRole> roles, String roleAlias) {
        if (!StringUtils.isEmpty(roleAlias)) {
            for (SysRole role : roles) {
                if (roleAlias.equals(role.getRoleKey())) {
                    return true;
                }
            }
        }
        return false;
    }
}
